<?php

!defined('ADMIN') && exit('Not allowed to directly view page');
//status'0 '待解决' 1 已解决 2精彩 -1不显示
//默认全部问题
if(!isset($status)){
	$status = 0;
}
if(!isset($tid2)){
	$tid2 = 0;
}
if(!isset($tid)){
	$tid = 0;
}
if(!isset($editor)){
	$editor = "all";
}

if(empty($action))
{
	require_once(DEDEINC.'/datalistcp.class.php');

		//所有专家
	$query="select id,uname from `#@__admin` ";
	$dsql->Execute('me',$query);
	while($row = $dsql->GetArray()){
		$editors[$row['id']]=$row['uname'];
	}

	$wheresql = $status == -1 ? " where 1 " : " where  ask.status=$status ";
	if($tid2){
		$wheresql .= " and ask.tid2='$tid2' ";
	}else if($tid){
		$wheresql .= " and ask.tid='$tid' ";
	}
	$wheresql .=$editor == "all"? " " :" and ans.uid= $editor";

	$query = "select ask.*,ans.username ans_uname,ans.uid ans_uid from `#@__ask` ask left join `#@__askanswer` ans on ask.id=ans.askid $wheresql order by id desc";

	updatecount();
	$dlist = new DataListCP();
	$dlist->pageSize = 20;
	$dlist->SetParameter("do",'ask_admin');
	$dlist->SetParameter("tid",$tid);
	$dlist->SetParameter("tid2",$tid2);
	$dlist->SetParameter("status",$status);
	$dlist->SetParameter("editor",$editor);
	$dlist->SetTemplet(DEDEADMIN."/templets/ask_admin.htm");
	$dlist->SetSource($query);
	$dlist->Display();
	exit();
}
else if($action == 'edit')
{
	if($step=="2" ){
		$solvetime=$dateline+3600*rand(8,24);
		//$solvetime=time();
		$kind=$kind==1?" `status`=1 ":($kind==2?" `status`=2 ":"`digest`=1");
		$uid = $cuserLogin->getUserID();
		$uname=$cuserLogin->getUserName();
		$ip = GetIP();
		$query="update `#@__ask` set `tid`= $tid ,`tidname`=\"$tidname\" , `tid2`= $tid2 ,`tid2name`=\"$tid2name\" , `title`=\"$asktitle\" , `content` =\"$askcontent\" , `solvetime`=$solvetime ,`extra`=\"\" , $kind  where `id`=$id";
		if($dsql->ExecuteNoneQuery2($query)!="-1"){
			if($ac=="modify"){
				$query="update `#@__askanswer`  set `uid`='$uid', `tid`= $tid , `tid2`= $tid2 ,`username`='$uname',`userip`='$ip',`content`='$answer',`doctor`='$docname',`photo`='$docphone' where `askid`=$id";
			}else
				$query="insert into `#@__askanswer`  values ('',$id,1,'$asktid','$asktid2','$uid','$uname','','','1','$ip','$solvetime','','$answer',1,'$docname','$docphone') ";
			$yy = $dsql->ExecuteNoneQuery2($query);

			if($yy!="-1"){
				ShowMsg("成功回复问题！",'?do=ask_admin&tid='.$tid.'&tid2='.$tid2.'&status='.$status.'&editor'.$editor);
				exit();
			}else{
				ShowMsg("回复 ！不成功 ！",'?do=ask_admin&tid='.$tid.'&tid2='.$tid2.'&status='.$status.'&editor'.$editor);
				exit();
			}
		}

	}else{
		$query = "select * from `#@__ask` where `id`=$id";
		$ask = $dsql->GetOne($query);
		$query = "select * from `#@__askanswer` where `askid`=$id";
		$askanswer = $dsql->GetOne($query);
		if($askanswer) $ac="modify"; else   $ac="add";

		include('ask_doctor.php');
		$doctors 	=	$doctor[$ask['tid']];
		if($askanswer['photo'] == ""){
			$key = array_keys($doctors);
			$askanswer['doctor']	=	$key[0];
			$askanswer['photo']		=	$doctors[$key[0]];
		}
		include(DEDEADMIN."/templets/ask_edit.htm");
		exit();
	}
}
else if($action == 'delete')
{
	CheckPurview('a_Del,a_AccDel,a_MyDel,sys_ArcBatch');
	require_once(DEDEINC."/oxwindow.class.php");

	if(empty($fmdo))
	{
		$fmdo = "";
	}
	if($fmdo=="yes")
	{
		if($qstr=='')
		{
			ShowMsg("参数无效！",'-1');
			exit();
		}
		$qstrs = explode("`",$qstr);
		$okaids = Array();
		foreach($qstrs as $aid)
		{
			if(!isset($okaids[$aid]))
			{
				$dsql->ExecuteNoneQuery("delete from `#@__ask` where id='$aid' ");
				$dsql->ExecuteNoneQuery("delete from `#@__askanswer` where askid='$aid' ");
			}else{
				$okaids[$aid] = 1;
			}
		}
		updatecount();
		ShowMsg("成功删除指定的问题！",'?do=ask_admin');
		exit();
	}//确定刪除操作完成

	//删除确认消息
	$wintitle = "文档管理-删除问题";
	$wecome_info = "<a href='".$ENV_GOBACK_URL."'>文档管理</a>::删除问题";
	$win = new OxWindow();
	$win->Init("?do=ask_admin","js/blank.js","POST");
	$win->AddHidden("fmdo","yes");
	$win->AddHidden("action",$action);
	$win->AddHidden("qstr",$qstr);
	$win->AddHidden("aid",$aid);
	$win->AddTitle("你确实要删除“ $qstr  ”这些问题？");
	$winform = $win->GetWindow("ok");
	$win->Display();
	exit();
}
//推荐
else if($action == 'digest')
{
	CheckPurview('a_Commend,sys_ArcBatch');
	if($qstr=='')
	{
		ShowMsg("参数无效！",'-1');
		exit();
	}
	$qstrs = explode("`",$qstr);
	foreach($qstrs as $aid)
	{
		$aid = ereg_replace("[^0-9]","",$aid);
		if ($aid == ""){ continue; }
		$dsql->ExecuteNoneQuery2("update `#@__ask` set `digest`='1' where id=$aid");
	}

	ShowMsg("成功把所选的问题设为推荐！",'?do=ask_admin');
	exit();
}
else
{  //精彩 跟 回收站
	CheckPurview('a_Commend,sys_ArcBatch');
	if($action=="deal"){
		$type=1; //已处理
	}else if($action=="good"){
		$type=2; //精彩
	}else if($action=="hold"){
		$type=-1; //回收站
	}
	if($qstr==""){
		ShowMsg("参数无效！",'-1');
		exit();
	}
	$qstrs = explode("`",$qstr);
	foreach($qstrs as $aid)
	{
		$aid = ereg_replace("[^0-9]","",$aid);
		if($aid==""){ continue; }
	//	echo "Update `#@__ask` set status='$type' where id='$aid'";
		$dsql->ExecuteNoneQuery2("update `#@__ask` set status='$type' where id='$aid'");
	}
	ShowMsg("状态更改成功！",'?do=ask_admin');
	exit();
}

function updatecount()
{
	global $dsql;
	$dsql->SetQuery("select id, reid from `#@__asktype` ");
	$dsql->Execute('asktype');
	while($row = $dsql->getarray('asktype'))
	{
		if($row['reid'] == 0)
		{
			$dsql->SetQuery("select count(*) as dd from `#@__ask` where tid='{$row['id']}' ");
			$dsql->Execute('top');
			$asknum = $dsql->getarray('top');
			$dsql->ExecuteNoneQuery("update `#@__asktype` set asknum='{$asknum['dd']}' where id='{$row['id']}' ");
		}
		else
		{
			$dsql->SetQuery("select count(*) as dd from `#@__ask` where tid2='{$row['id']}' ");
			$dsql->Execute('sub');
			$asknum = $dsql->getarray('sub');
			$dsql->ExecuteNoneQuery("update `#@__asktype` set asknum='{$asknum['dd']}' where id='{$row['id']}' ");
		}
	}
}

?>